package mas;

/** Test harness demonstrating UserAgents 
 * 
 * Advanced Java Programming - ICA 2 Middleware 
 * @author Carl Morphet, Matt Constable, Stephen Pammenter
 * Teesside University
 * Created: Mar 20, 2012
 */
public class TestMAS
{
    /** The terminating string, which when passed in a Message object will stop 
     * further queuing */
    private static final String SHUTDOWN_REQ = "SHUTDOWN";
    
    public static void main(String[] args)
    {
        System.out.println("-------------Test 1----------------");
        System.out.println("Agent -> Portal -> Portal -> Agent\n");
        messageTest();
        
        System.out.println("\n------------Test 2---------------");
        System.out.println("Agent -> Portal -> Agent");
        messageTest2();
        
        System.out.println("\n------------Test 3---------------");
        System.out.println("Exiting gracefully");
        messageTest3();
    }
    
    /** This method shows correct message routing from one UserAgent to its 
     * associated portal, which passes the message object to the intended 
     * recipient through the recipients associated portal
     */
    private static void messageTest()
    {
        /* Creating and instantiating two UserAgents with their own portals */
        Portal portal1 = new Portal("Portal 1");
        UserAgent a = new UserAgent("A");
        
        Portal portal2 = new Portal("Portal 2");
        UserAgent b = new UserAgent("B");
        
        /* Creating a message object and registering the UserAgents to their
         * portals */
        portal1.registerAgent(a.getName(), a);
        portal1.registerAgent(portal2.getPortalName(), portal2);
        
        portal2.registerAgent(b.getName(), b);
        portal2.registerAgent(portal1.getPortalName(), portal1);
        
        Message testMsg = new Message("01", a.getName(), b.getName(), "Test message filler");
        
        /* Sending the message from UserAgent a to UserAgent b via the portals */
        a.sendMessage(testMsg);
    }
    
    /** This method creates and instantiates two UserAgents which share the same
     * portal, then demonstrates message routing between them 
     */
    private static void messageTest2()
    {
        UserAgent a = new UserAgent("A");
        UserAgent b = new UserAgent("B");
        
        Portal portal = new Portal("Portal 1");
        
        portal.registerAgent(a.getName(), a);
        portal.registerAgent(b.getName(), b);
        
        Message message = new Message("02", a.getName(), b.getName(), "More test message filler");
        
        a.sendMessage(message);
    }
    
    /** Method tests the MAS systems ability to exit gracefully through the 
     * sending of a message with the ID set to the shut down command
     */
    private static void messageTest3()
    {
        UserAgent a = new UserAgent("A");
        UserAgent b = new UserAgent("B");
        
        Portal portal = new Portal("Portal 1");
        
        portal.registerAgent(a.getName(), a);
        portal.registerAgent(b.getName(), b);
        
        Message message = new Message(SHUTDOWN_REQ, a.getName(), b.getName(), "Shut down everything.");
        
        a.sendMessage(message);
    }
}